<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title></title>
  <link rel="stylesheet" type="text/css" href="css/common.css"/>
  <link rel="stylesheet" type="text/css" href="css/article_add.css"/>
  <link rel="stylesheet" href="layui/css/layui.css" media="all"/>
</head>
<body>
<form action="" method="post" id="form" class="search_box">
  <div>
    <span>推广人员：</span>
    <input type="text" name="name" id="name" placeholder="请输入推广人员"/>
    <input type="hidden" name="id" id="id" placeholder="请输入推广人员"/>
  </div>
  <div>
    <span>推广内容：</span>
    <select name="typeid" id="typeid" placeholder="请选择推广内容"></select>
  </div>
  <div>
    <span>有效期限：</span>
    <input type="text" name="indate" id="indate" placeholder="请选择有效期限"/>
  </div>

  <button type="submit" class="sub" data-tip="二维码列表">提交</button>
  <button type="reset" class="reset" style="display: none;"></button>
</form>

<script src="js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" src="layui/layui.js"></script>

<script src="js/jquery.cookie.js" type="text/javascript" charset="utf-8"></script>
<script>
var params = {
    name: '',
    //姓名
    status: '0',
    limit: 10,
    //记录数
    offset: 1 //页码
};

layui.use(['element', 'form', 'laydate', 'layedit'],
function() {
   var id= $.cookie("codeId");
   if(id && id!=''){
    $.ajax({
            type: "GET",
            url: "/code/"+id,
            cache: false,
            contentType: 'application/json;utf-8',
            processData: false,
            success: function(data) {
                if (data.rel) {
                    $("#name").val(data.result.name);
                     $("#name").attr('disabled', 'disabled');
                    $("#id").val(id);
                    $("#typeid").val(data.result.typeid);
                     $("#typeid").attr('disabled', 'disabled');
                    $("#indate").val(data.result.indate);
                }
             }
     });

   }
   $.cookie('codeId', '');
    var laydate = layui.laydate;
    laydate.render({
        elem: '#indate' //指定元素
    });
    initTypeSelect();

    $('#form').submit(function(e) {
        $.ajax({
            type: "POST",
            url: "/code/edit",
            data: JSON.stringify({
                id: $("#id").val(),
                name: $("#name").val(),
                status: "0",
                indate: $("#indate").val(),
                typeid: $("#typeid").val()
            }),
            cache: false,
            contentType: 'application/json;utf-8',
            processData: false,
            success: function(data) {
                if (data) {
                    if (data.rel) {
                        layui.use('layer',
                        function() {
                            var layer = layui.layer;

                            layer.msg(data.msg, {
                                icon: 6
                            },
                            function() {
                                var text = $('.sub').attr('data-tip');
                                $(window.parent.$(".acrossTab li").each(function() {
                                    if ($(this).children('span').text() == text) {
                                        $(window.parent.$("aside").attr('data-tip', 1));
                                        $(this).click();
                                    } else {
                                        $(window.parent.$("aside li").each(function() {
                                            if ($(this).text() == text) {
                                                $(this).click();
                                            }
                                        }));
                                    }
                                }));
                                $('.reset').click();
                            })
                        })
                    } else {
                        layui.use('layer',
                        function() {
                            var layer = layui.layer;
                            layer.msg(data.msg, {
                                icon: 5
                            })
                        })
                    }

                } else {
                    layui.use('layer',
                    function() {
                        var layer = layui.layer;
                        layer.msg('保存失败', {
                            icon: 5
                        })
                    })
                }
            }
        });
        e.preventDefault();
    });
});

$('.lawyer_name').on('input',
function() {
    $('.name-list').hide();
    var name = $(this).val();
    if (name.length == 0) {
        return false;
    }
    $.ajax({
        type: "get",
        url: "lawyer/list",
        data: {
            name: name
        },
        dataType: "json",
        success: function(data) {
            var name_list = '';
            if (data.rows.length != 0) {
                for (var i = 0; i < data.rows.length; i++) {
                    name_list += '<li>' + data.rows[i].name + '</li>'
                }

                $('.name-list').html(name_list).show();

                $('.name-list li').click(function() {

                    $('.lawyer_name').val($(this).text());

                    $(this).parent('ul').html('').hide();
                })

            } else {
                $('.name-list').html('').hide();
            }
        }
    });
});

function initTypeSelect() {
    $.ajax({
        url: '/type/list',
        type: 'get',
        data: params,
        dataType: "json",
        success: function(data) {
            var contentHtml = "";
            if (data.rows) {
                for (var i = 0; i < data.rows.length; i++) {
                    var obj = data.rows[i];
                    contentHtml += "<option value='" + obj.id + "'>" + obj.name + "</option>"
                }
            }
            $("#typeid").html(contentHtml);
        }
    });
}





</script>
</body>
</html>
